package com.atguigu.day22.tcpsocket.s3;

import java.io.IOException;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

/*
 客户端      服务端
 ABCD   ---> ABCD
 BCBA   <--- DCBA
 -----
 1234        1234
 4321        4321

 */
public class Client3 {
    public static void main(String[] args) {
        System.out.println("--------Client3-----------");
        //1.创建客户端对象
        try (Socket socket = new Socket(InetAddress.getLocalHost(), 8989);
             //2.准备流对象
             //2.1 打印流
             PrintStream ps = new PrintStream(socket.getOutputStream());
             //2.2 Scanner 读取服务端反馈的数据
             Scanner sc = new Scanner(socket.getInputStream());
             //2.3 键盘输入数据
             Scanner input = new Scanner(System.in);

        ) {
            //3.循环 写 读
            while (true){
                System.out.println("请您输入一句话");
                String message = input.next();
                //写出数据
                ps.println(message);
                //读取反馈的数据
                String line = sc.nextLine();
                System.out.println("服务端反馈的数据 = " + line);
            }

        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
